ResponseOfLastAJAX = "";

function DeleteObject(serializedObject)
{
	CreateLoadingAnimation();
	
	var defer = $.Deferred();
	FinalXML = "<objects>" + serializedObject + "</objects>";
	$.ajaxSetup
	({
		cache: "true"
	});

	$.ajax
	({
		type: "DELETE",
		url:"svc/crud",
		cache:true,
		contentType:"text/xml",
		dataType: "text",
		data:FinalXML,

		success: function(XMLresp)
		{
			CreateInfoWindow("Record successfully deleted!");
			defer.resolve("success");
			DeleteLoadingAnimation();
		},

		error:function(XMLHttpRequest, textStatus, errorThrown)
		{
			DeleteLoadingAnimation();
			if(XMLHttpRequest.responseText == null) XMLHttpRequest.responseText = "Unknown!";
			CreateErrorWindow("Couldn't delete record."
					+ " Check connection with server. " +
					"Or try reloading the windows to see if the record wasn't deleted by anyone else."+
					"Cause: " + XMLHttpRequest.responseText);
			defer.reject("fail");
		}

	});
	
	
	return defer;
}

function UpdateObject(serializedObject)
{
	CreateLoadingAnimation();
	
	var defer = $.Deferred();
	FinalXML = "<objects>" + serializedObject + "</objects>";
	$.ajaxSetup
	({
		cache: "true"
	});

	$.ajax
	({
		type: "PUT",
		url:"svc/crud",
		cache:true,
		contentType:"text/xml",
		dataType: "text",
		data:FinalXML,

		success: function(XMLresp)
		{
			DeleteLoadingAnimation();
			CreateInfoWindow("Record successfully updated!");
			defer.resolve("success");
		},

		error:function(XMLHttpRequest, textStatus, errorThrown)
		{
			DeleteLoadingAnimation();
			if(XMLHttpRequest.responseText == null) XMLHttpRequest.responseText = "Unknown!";
			CreateErrorWindow("Couldn't update record."
					+ " Check connection with server. " +
					"Cause: " + XMLHttpRequest.responseText);
			defer.reject("fail");
		}

	});
	return defer;
}

function CreateObject(serializedObject, ServicePath)
{
	CreateLoadingAnimation();
	var defer = $.Deferred();
	FinalXML = "<objects>" + serializedObject + "</objects>";
	$.ajaxSetup
	({
		cache: "true"
	});

	$.ajax
	({
		type: "POST",
		url:ServicePath,
		cache:true,
		contentType:"text/xml",
		dataType: "text",
		data:FinalXML,

		success: function(XMLresp)
		{
			DeleteLoadingAnimation();
			CreateInfoWindow("Record successfully updated!");
			defer.resolve("success");
		},

		error:function(XMLHttpRequest, textStatus, errorThrown)
		{
			DeleteLoadingAnimation();
			if(XMLHttpRequest.responseText == null) XMLHttpRequest.responseText = "Unknown!";
			CreateErrorWindow("Couldn't create record."
					+ " Check connection with server. " +
					"Cause: " + XMLHttpRequest.responseText);
			defer.reject("fail");
		}

	});
	return defer;
}

function GetObjects(url)
{
	CreateLoadingAnimation();
	var defer = $.Deferred();
	$.ajaxSetup
	({
		cache: "true"
	});
	
	$.ajax
	({
		type: "GET",
		url:url,
		cache:true,
		dataType: "text",
		contentType: "text/xml",
		success: function(XMLresp)
		{
			DeleteLoadingAnimation();
			ResponseOfLastAJAX = XMLresp;
			defer.resolve("success");
		},
		
		error:function(XMLHttpRequest, textStatus, errorThrown)
		{
			DeleteLoadingAnimation();
			ResponseOfLastAJAX = XMLHttpRequest.responseText;
			defer.reject("fail");
		}
		
		
	});
	return defer;
}

function GetLastAJAXResponse()
{
	return ResponseOfLastAJAX;	
}